/*
 * Created by JFormDesigner on Fri Nov 01 19:50:56 CST 2024
 */

package com.hafu.view.Login;

import java.awt.event.*;
import com.hafu.service.IUserService;
import com.hafu.service.impl.UserServiceImpl;
import com.hafu.util.StringUtil;
import com.hafu.view.MainFrm;
import com.hafu.view.inventory.InventoryFrm;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author 20721
 */
public class LoginFrm extends JFrame {

    public LoginFrm() {
        initComponents();
    }


    private UserServiceImpl userService = new UserServiceImpl();

    private void button1MouseClicked(MouseEvent e) throws SQLException {
        // TODO add your code here
        String username = this.name.getText();
        String password = new String(pwd.getPassword());
        if (StringUtil.isEmpty(username)) {
            JOptionPane.showMessageDialog(null, "用户名不能为空");
        } else if (StringUtil.isEmpty(password)) {
            JOptionPane.showMessageDialog(null, "密码不能为空");
        }
        Boolean isLogin = userService.isLogin(username, password);
        if (isLogin) {
            JOptionPane.showMessageDialog(null, "登录成功");
            this.dispose();
            MainFrm mainFrm = new MainFrm();
            mainFrm.setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "用户名或密码错误");
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        textField1 = new JLabel();
        textField2 = new JLabel();
        textField3 = new JLabel();
        name = new JTextField();
        button1 = new JButton();
        pwd = new JPasswordField();

        //======== this ========
        var contentPane = getContentPane();

        //---- textField1 ----
        textField1.setText("\u6cb3\u5357\u8d22\u653f\u91d1\u878d\u5b66\u9662\u56fe\u4e66\u7ba1\u7406\u7cfb\u7edf");
        textField1.setHorizontalAlignment(SwingConstants.CENTER);
        textField1.setFont(textField1.getFont().deriveFont(textField1.getFont().getStyle() | Font.BOLD, textField1.getFont().getSize() + 12f));

        //---- textField2 ----
        textField2.setText("\u7528\u6237\u540d:");

        //---- textField3 ----
        textField3.setText("\u5bc6\u7801:");

        //---- button1 ----
        button1.setText("\u767b\u5f55");
        button1.setForeground(Color.magenta);
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                try {
button1MouseClicked(e);} catch (SQLException ex) {
    throw new RuntimeException(ex);
}
            }
        });

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(308, 308, 308)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(textField2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(textField3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGap(38, 38, 38)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                .addComponent(name, GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                                .addComponent(button1)
                                .addComponent(pwd, GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(269, 269, 269)
                            .addComponent(textField1)))
                    .addContainerGap(272, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(91, 91, 91)
                    .addComponent(textField1)
                    .addGap(65, 65, 65)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(textField2)
                        .addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                    .addGap(38, 38, 38)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(pwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField3))
                    .addGap(76, 76, 76)
                    .addComponent(button1)
                    .addContainerGap(54, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel textField1;
    private JLabel textField2;
    private JLabel textField3;
    private JTextField name;
    private JButton button1;
    private JPasswordField pwd;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
